<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.applicationName" placeholder="名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="filters.passId" placeholder="状态"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getApps">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="apps" highlight-current-row v-loading="listLoading"
                  style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column prop="userName" label="请假人" width="80">
            </el-table-column>
            <el-table-column prop="reason" label="请假原因" width="180">
            </el-table-column>
            <el-table-column prop="startTime" label="开始时间" width="160">
            </el-table-column>
            <el-table-column prop="endTime" label="结束时间" width="160" >
            </el-table-column>
            <el-table-column prop="day" label="请假天数" width="100" >
            </el-table-column>
            <!--<el-table-column prop="currentProcess" @click="curProImg(scope.row)" label="查看进度" width="100" >-->
            <!--</el-table-column>-->
            <el-table-column label="操作" width="350">
                <template scope="scope">
                    <el-button type="info" size="small" @click="curProImg(scope.row)">查看进度</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-pagination layout="total, sizes, prev, pager, next" @current-change="handleCurrentChange" @size-change="handleSizeChange" :page-size="page.pageSize"
                           :total="page.total" style="float:right;">
            </el-pagination>
        </el-col>
        <el-dialog title="当前进度" :visible.sync="showProVisible"  :close-on-click-modal="true">
            <img :src="showProUrl">
        </el-dialog>
        <el-dialog title="新增" :visible.sync="addFormVisible"  :close-on-click-modal="true" @close='closeAddDialog'>
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="请假人" prop="applicationName">
                    <el-input v-model="addForm.userName" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="请假时间" prop="passToken">
                    <el-date-picker
                            v-model="addForm.daterange"
                            type="daterange"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            @change="change">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="请假天数" prop="description">
                    <el-input v-model="addForm.day"></el-input>
                </el-form-item>
                <el-form-item label="请假原因" prop="description">
                    <el-input type="textarea" v-model="addForm.reason"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addLeave" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    import NProgress from 'nprogress'
    import {addLeave, getLeaveList} from "@/api/api";
    import {deleteLeave} from "../../api/api";


    export default {
        inject:['reload'],
        data() {
            return {
                showProUrl:'',
                showProVisible:false,
                addFormVisible: false,//新增界面是否显示
                getLeaveCategoryList:[{"value":1,"label":"事假"},{"value":2,"label":"年假"}],
                filters: {
                    applicationName: '',
                    passId:''
                },
                apps: [],
                page: {
                    pageNo: 1,
                    pageSize: 10,
                    total: 400
                },
                listLoading: false,
                sels: [],//列表选中列
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [
                        {required: true, message: '请输入名字', trigger: 'blur'}
                    ],
                    reason: [
                        {required: true, message: '请输入请假原因', trigger: 'blur'}
                    ],
                    startTime: [
                        {required: true, message: '请输入开始时间', trigger: 'blur'}
                    ],
                    endTime: [
                        {required: true, message: '请输入结束时间', trigger: 'blur'}
                    ],
                    day: [
                        {required: true, message: '请输入请假天数', trigger: 'blur'}
                    ],
                },
                //编辑界面数据
                editForm: {
                    daterange:'',
                    startTime: '',
                    endTime: '',
                    day:'',
                    reason:'',
                    name: '',
                },


                addLoading: false,
                addFormRules: {
                    userName: [
                        {required: true, message: '请输入名字', trigger: 'blur'}
                    ],
                    reason: [
                        {required: true, message: '请输入请假原因', trigger: 'blur'}
                    ],
                    startTime: [
                        {required: true, message: '请输入开始时间', trigger: 'blur'}
                    ],
                    endTime: [
                        {required: true, message: '请输入结束时间', trigger: 'blur'}
                    ],
                    day: [
                        {required: true, message: '请输入请假天数', trigger: 'blur'}
                    ],
                },
                //新增界面数据
                addForm: {
                    daterange:'',
                    startTime: '',
                    endTime: '',
                    day:'',
                    reason:'',
                    userName: '',
                    processType:1,
                    pdfKey:'',
                    currentProcess:''
                }

            }
        },
        methods: {
            curProImg(data){
              //let aa= data.currentProcess
                this.showProVisible=true
                this.showProUrl="/api/vue/CurProImg?pdfKey="+data.pdfKey+"&proInsId="+data.currentProcess
            },
            addLeave(){
               // let cc= this.addForm
               //  debugger
               //  alert(cc.pdfKey+"---"+cc.currentProcess)
              addLeave(this.addForm).then(res=>{
                  //apps=res.data;
                  this.addFormVisible=false
                  this.$message({
                      type:"success",
                      message:"success"
                  })
                  this.reload()
              }).catch(e=>{
                  console.log(e);
              });
            },
            getLeaveListReq(){
                const data={
                    userName:JSON.parse(sessionStorage.getItem("mockUser")).userName
                }
                getLeaveList(data).then(res => {
                    //自己的业务逻辑代码：服务端返回的参数都封装在了data里面
                    this.apps=res.data;
                    console.log(res.data);
                    console.log(this.apps);
                }).catch(e => {
                    console.log("请求错误");
                    console.log(e);
                    //自己的业务逻辑代码：服务端返回的异常都封装在了e里面
                });
            },
            change(val) {
                this.addForm.startTime = val[0].getTime();
                this.addForm.endTime = val[1].getTime();
                this.day = Number.parseInt((val[1].getTime() - val[0].getTime()) / 1000 / 60 / 60 / 24) + 1;
                this.addForm.day = this.day;
            },
            //显示新增界面
            handleAdd() {
               this.addForm.userName= JSON.parse(sessionStorage.getItem("mockUser")).userName,
                this.addFormVisible = true;
            },
            closeAddDialog() {
                this.$refs['addForm'].resetFields();
            },
            statusFormat(row, column) {
                if (row.releaseStatus === 1) {
                    return '已上线'
                }
                else  {
                    return '未上线'
                }
            },
            typeFormat(row, column) {
                if (row.applicationType === 1) {
                    return '业务应用'
                }else if (row.applicationType === 2) {
                    return '系统应用'
                }
                else  {
                    return '第三方应用'
                }
            },
            handleCurrentChange(val) {
                this.page.pageNo = val;
                this.getApps();
            },
            handleSizeChange(val) {
                this.page.pageSize = val;
                this.getApps();
            },
            //获取应用列表
            getApps() {
                let para = {
                    pageNo: this.page.pageNo,
                    pageSize: this.page.pageSize,
                    applicationName: this.filters.applicationName,
                    passId:this.filters.passId
                };
                this.listLoading = true;
                NProgress.start();
                    this.listLoading = false;
                    NProgress.done();

            },
            //删除
            handleDel: function (index, row) {
                    this.$confirm('确认删除该记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        deleteLeave(row.id).then((res) => {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.reload()
                            console.log(res.errmsg)
                        }).catch(error => {
                            console.log(error);
                        });
                    })}
            },
        mounted() {
            this.getApps();
            this.getLeaveListReq();
        }
    }

</script>

<style>
    .el-dialog > .el-dialog__body {
        padding: 0px;
        margin: 20px 20px;
        border: 1px  transparent;
    }
</style>
